var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({

  data: {
    imgUrl: globalData.imgUrl,
    user:[],
    rechargeFlag:true,
    money:"",
    orderNo:"",
  },
  rechargeFlag:function(){
    this.setData({ rechargeFlag: !this.data.rechargeFlag, });
    this.setData({ money: "" })
  },
  money:function(e){
    let money = e.detail.value;
    this.setData({ money: money})
  },
  confirm: function () {
    this.setData({ rechargeFlag: true });
    if (this.data.money != '' && Number(this.data.money) > 0) {
      this.submitOrder();
    }else{
      wx.showModal({title: '提示',content: '充值金额必须大于0！',})
    }
  },
  //提交订单
  submitOrder: function () {
    var that = this;
    let total = this.data.total;
    let params = new Object();
    params.money = this.data.money;
    console.log("订单提交参数" + JSON.stringify(params));
    commonUtil.showLoading("提交中...");
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded"},
      url: httpsUrl + "/order/memberRecharge",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        console.log("提交订单返回:" + JSON.stringify(res));
        res = res.data;
        if (res.result == 0) {
          that.setData({ orderNo: res.data })
          that.goPay();
          //that.paySuccess();
        } else {
          commonUtil.showModal(res.msg);
        }
      }
    })
  },
  //获取openid
  goPay: function () {
    let total = this.data.money;//合计为0，跳过在线支付
    if (total > 0) {
      var that = this;
      wx.login({
        success: function (res) {
          wx.showLoading({ title: '请稍等...', })
          wx.request({
            header: { "content-Type": "application/x-www-form-urlencoded" },
            url: httpsUrl + "/wechat/getOpenId",
            dataType: "json",
            method: "POST",
            data: { "code": res.code, "agentCode": globalData.agentCode },
            success: function (res) {
              wx.hideLoading();
              res = res.data;
              console.log("获取openid返回:" + JSON.stringify(res));
              if (res.result == 0) {
                let data = JSON.parse(res.data);
                console.log(data.openid);
                that.setData({ openid: data.openid })
                that.pay();
              } else {
                console.log("获取openid失败：" + res.msg);
              }
            }
          })
        }
      })
    } else if (total == 0) {
      this.paySuccess();
    }
  },
  //支付
  pay: function () {
    let that = this;
    let params = new Object();
    params.orderNo = this.data.orderNo;
    params.openid = this.data.openid;
    console.log(JSON.stringify(params));
    commonUtil.showLoading("去支付...");
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/order/payParam",//统一下单返回支付参数
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        console.log("统一下单返回" + JSON.stringify(res));
        res = res.data;
        if (res.result == 0) {
          var data = res.data;
          //调起支付
          wx.requestPayment(
            {
              'timeStamp': data.timeStamp,
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': 'MD5',
              'paySign': data.paySign,
              'success': function (res) {
                console.log("支付调起成功");
                // that.paySuccess();
              },
              'fail': function (res) {
                console.log("用户取消支付");
                wx.showToast({title: '您已取消支付！',})
                // wx.switchTab({
                //   url: '../../order/orderList/orderList',
                // })
              },
              'complete': function (res) { }
            })
        } else {
          commonUtil.showModal("支付失败：" + res.msg);
        }
      }
    })
  },

  paySuccess: function () {
    wx.navigateTo({
      url: '../../order/paySuccess/paySuccess',
    })
  },
  loadData: function () {
    let that = this;
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/member/info",
      dataType: "json",
      method: "GET",
      // data: { "openId": data.openid },
      success: function (res) {
        res = res.data;
        console.log("userInfo:" + JSON.stringify(res));
        if (res.result == 0) {
          var user = res.data;
          user.amount = user.amount.toFixed(2);
          user.commission = user.commission.toFixed(2);
          user.commission_frozen = user.commission_frozen.toFixed(2);
          user.total = (Number(user.amount) + Number(user.commission)).toFixed(2);
          that.setData({ user: user })
        }
      }
    })
  },
  cardList:function(){
    wx.navigateTo({
      url: '../cardList/cardList',
    })
  },
  putforward:function(){
    wx.navigateTo({
      url: '../putforward/putforward',
    })
  },
  rechargeList:function(){
    wx.navigateTo({
      url: '../rechargeList/rechargeList',
    })
  },
  commissionList:function(){
    wx.navigateTo({
      url: '../commissionList/commissionList',
    })
  },
  putforwardList:function(){
    wx.navigateTo({
      url: '../putforwardList/putforwardList',
    })
  },
  onLoad: function (options) {
    this.loadData();
  },
  onShow: function () {
    this.loadData();
  },

})